package com.wshl.aijiuunion.controller.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.NetUrl;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.controller.activity.PersonDataActivity;
import com.wshl.aijiuunion.controller.activity.RechargeActivity;
import com.wshl.aijiuunion.controller.activity.SelectTypeActivity;
import com.wshl.aijiuunion.controller.activity.SetActivity;
import com.wshl.aijiuunion.controller.activity.ShengjiActivity;
import com.wshl.aijiuunion.controller.activity.TujianActivity;
import com.wshl.aijiuunion.controller.activity.VideoListActivity;
import com.wshl.aijiuunion.controller.activity.WebActivity;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.utils.BarUtils;
import com.wshl.aijiuunion.utils.GlideUtils;
import com.wshl.aijiuunion.utils.share.LoginOutUtils;
import com.wshl.aijiuunion.view.bean.UserinfoBean;
import com.wshl.aijiuunion.view.widget.Keyboard;
import com.wshl.aijiuunion.view.widget.PayEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created buy 风凊扬 on 2019/10/21 0021
 */
public class MineFragment extends TabFragment {
    @BindView(R.id.img_avatar)
    ImageView imgAvatar;
    @BindView(R.id.tv_dengji)
    TextView tvDengji;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    Unbinder unbinder;
    @BindView(R.id.tv_yue)
    TextView tvYue;
    @BindView(R.id.tv_shebei)
    TextView tvShebei;
    @BindView(R.id.tv_tuijian)
    TextView tvTuijian;
    @BindView(R.id.tv_pingjia)
    TextView tvPingjia;
    @BindView(R.id.tv_zuji)
    TextView tvZuji;
    @BindView(R.id.tv_shoucang)
    TextView tvShoucang;
    @BindView(R.id.tv_yijian)
    TextView tvYijian;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.tv_shenqing)
    TextView tvShenQing;
    private String url = "http://cncttv.com/uploads/avatar/mfgQZ9XUHMXEZ8FbMO9uG3lAjW1NbImJdYhHGLrK.jpeg";
    private String url2 = "http://img61.ddimg.cn/upload_img/00791/xmj/750x310_dl_0626-1561722481.jpg";
    private AlertDialog dialog;

    private RequestModel model;

    @Override
    public void initImmersionBar() {
        BarUtils.setStatusBarForFragment(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        LogUtils.i("sha1:" + AppUtils.getAppSignatureSHA1());
        LogUtils.i("minefragment--onCreateView");
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        model = new RequestModel();


    }


    @Override
    public void onResume() {
        super.onResume();
        if (!NetworkUtils.isConnected()) {
            setuserinfo();
        } else {

            String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
            tvYue.setText(0.00 + "元\n我的余额");
            tvShebei.setText(0 + "台\n我签约的设备");
            tvTuijian.setText(0 + "人\n我的推荐");
            if (!TextUtils.isEmpty(token)) {

                getUserinfo();
            }

        }

    }

    private void setuserinfo() {
        SPUtils instance = SPUtils.getInstance();
        String avatar = instance.getString(GlobConstant.AVATAR);
        String nickname = instance.getString(GlobConstant.NIAKNAME);
        String mobile = instance.getString(GlobConstant.MOBILE);
        String money = instance.getString(GlobConstant.MONEY);
        String type = instance.getString(GlobConstant.TYPE);
        String tuijian = instance.getString(GlobConstant.TUIJIAN);
        String shebieshu = instance.getString(GlobConstant.SHEBEISHU);

        tvYue.setText(money + "元\n我的余额");
        tvShebei.setText(shebieshu + "台\n我签约的设备");
        tvTuijian.setText(tuijian + "人\n我的推荐");
        if (!TextUtils.isEmpty(avatar)) {
            if (!avatar.startsWith("http")) {
                GlideUtils.loadCircleImage(mContext, NetUrl.IMG + avatar, imgAvatar);
            } else {
                GlideUtils.loadCircleImage(mContext, avatar, imgAvatar);
            }

        }

        tvNickname.setText(nickname);
        tvMobile.setText(mobile);

        switch (type) {
            case "1":
                tvDengji.setText("分公司");
                tvShenQing.setVisibility(View.GONE);
                break;
            case "2":
                tvDengji.setText("县级代理");
                tvShenQing.setVisibility(View.VISIBLE);
                break;
            case "3":
                tvDengji.setText("业务员");
                tvShenQing.setVisibility(View.VISIBLE);
                break;
            case "4":
                tvDengji.setText("普通用户");
                tvShenQing.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!NetworkUtils.isConnected()) {
                setuserinfo();
            } else {

                String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
                tvYue.setText(0.00 + "元\n我的余额");
                tvShebei.setText(0 + "台\n我签约的设备");
                tvTuijian.setText(0 + "人\n我的推荐");
                if (!TextUtils.isEmpty(token)) {

                    getUserinfo();
                }
            }
        }
    }

    private void getUserinfo() {

        model.getUserinfo(mContext, new RequestCallback() {
            @Override
            public void onSuccess(String response) {

                UserinfoBean userinfoBean = GsonUtils.fromJson(response, UserinfoBean.class);
                UserinfoBean.DataBean data = userinfoBean.getData();
                String money = data.getMoney();
                int equip_num = data.getEquip_num();//设备数
                int recommend_num = data.getRecommend_num();//推荐人数

                String avatar = data.getAvatar();
                String nickname = data.getName();
                String mobile = data.getPhone();
                String cover = data.getCover();
                String recommend_code = data.getRecommend_code();
                String payword = data.getPayword();

                int type = data.getType();//1分公司2县级代理3业务员4普通用户
                SPUtils instance = SPUtils.getInstance();
                instance.put(GlobConstant.TUIJIAN, recommend_num + "");
                instance.put(GlobConstant.SHEBEISHU, equip_num + "");
                instance.put(GlobConstant.MONEY, money + "");
                instance.put(GlobConstant.TYPE, type + "");
                instance.put(GlobConstant.MOBILE, mobile);
                instance.put(GlobConstant.RECOMMENCOVER, cover);
                instance.put(GlobConstant.RECOMMENDCODE, recommend_code);
                instance.put(GlobConstant.PAYWORD, payword);
                switch (type) {
                    case 1:
                        tvDengji.setText("分公司");
                        tvShenQing.setVisibility(View.GONE);
                        break;
                    case 2:
                        tvDengji.setText("县级代理");
                        tvShenQing.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        tvDengji.setText("业务员");
                        tvShenQing.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        tvDengji.setText("普通用户");
                        tvShenQing.setVisibility(View.VISIBLE);
                        break;
                }
                tvYue.setText(money + "元\n我的余额");
                tvShebei.setText(equip_num + "台\n我签约的设备");
                tvTuijian.setText(recommend_num + "人\n我的推荐");
                if (!TextUtils.isEmpty(avatar)) {
                    if (!avatar.startsWith("http")) {
                        GlideUtils.loadCircleImage(mContext, NetUrl.IMG + avatar, imgAvatar);
                    } else {
                        GlideUtils.loadCircleImage(mContext, avatar, imgAvatar);
                    }

                }
                tvNickname.setText(nickname);
                tvMobile.setText(mobile);


            }

            @Override
            public void onError(int code) {

            }

            @Override
            public void onIntentError() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.tv_kecheng, R.id.rl_tuijian, R.id.tv_shenqing, R.id.img_avatar, R.id.tv_yue, R.id.tv_shebei, R.id.tv_tuijian, R.id.tv_myorders, R.id.tv_pingjia, R.id.tv_zuji, R.id.tv_shoucang, R.id.tv_yijian, R.id.tv_set})
    public void onViewClicked(View view) {


        if (view.getId() == R.id.tv_shenqing) {

            ActivityUtils.startActivity(SelectTypeActivity.class);
            return;
        }

        String token = SPUtils.getInstance().getString(GlobConstant.TOKEN);
        if (TextUtils.isEmpty(token)) {
            LoginOutUtils.reLogin(mContext);
            return;
        }
        switch (view.getId()) {
            case R.id.tv_kecheng:
                ActivityUtils.startActivity(mContext, VideoListActivity.class);
                break;
            case R.id.img_avatar:
                ActivityUtils.startActivity(mContext, PersonDataActivity.class);
                break;
            case R.id.tv_yue:
                ActivityUtils.startActivity(RechargeActivity.class);
                break;
            case R.id.tv_shebei:
                startWeb(token, NetUrl.SHEBEI);
                break;
            case R.id.rl_tuijian:
                ActivityUtils.startActivity(TujianActivity.class);
                break;
            case R.id.tv_tuijian:
                startWeb(token, NetUrl.TUIJIAN);
                break;
            case R.id.tv_myorders:
                startWeb(token, NetUrl.MYORDER);
                break;
            case R.id.tv_pingjia:
                startWeb(token, NetUrl.PINGJIA);
                break;
            case R.id.tv_zuji:
                startWeb(token, NetUrl.ZUJI);
                break;
            case R.id.tv_shoucang:
                startWeb(token, NetUrl.COLLECT);
                break;
            case R.id.tv_yijian:
                startWeb(token, NetUrl.FANKUI);
                break;
            case R.id.tv_set:
                ActivityUtils.startActivity(mContext, SetActivity.class);
                break;
        }
    }

    private void startWeb(String token, String url) {
        if (TextUtils.isEmpty(token)) {
            ToastUtils.showShort("请登录");
            return;
        }
        Intent intent = new Intent(mContext, WebActivity.class);
        intent.putExtra(GlobConstant.WEBURL, url + token);
        ActivityUtils.startActivity(intent);
    }


    private static final String[] KEY = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "<<"};

    private void showPaydialog(String id, String paymodel) {
        //dialog = new Dialog(mContext, R.style.bottom_dialog);
        dialog = new AlertDialog.Builder(mContext, R.style.bottom_dialog).create();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pay, null);


        dialog.show();
        dialog.setContentView(inflate);
        Keyboard keyboard = inflate.findViewById(R.id.KeyboardView_pay);
        LinearLayout lldialog2 = inflate.findViewById(R.id.ll_dialog2);
        lldialog2.setVisibility(View.VISIBLE);
        inflate.findViewById(R.id.img_guanbi1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
        });
        inflate.findViewById(R.id.tv_forget_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //忘记密码

            }
        });
        keyboard.setKeyboardKeys(KEY);
        final PayEditText payEditText = inflate.findViewById(R.id.PayEditText_pay);
        keyboard.setOnClickKeyboardListener(new Keyboard.OnClickKeyboardListener() {
            @Override
            public void onKeyClick(int position, String value) {
                if (position < 11 && position != 9) {
                    payEditText.add(value);
                } else if (position == 11) {
                    payEditText.remove();
                } else if (position == 9) {
                }
            }
        }); /**
         *当密码输入完成时的回调
         */
        payEditText.setOnInputFinishedListener(new PayEditText.OnInputFinishedListener() {
            @Override
            public void onInputFinished(String password) {
                //判断密码是否正确

            }
        });
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = AbsListView.LayoutParams.MATCH_PARENT;
        lp.y = 0;
        //设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);

    }
}
